﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.popups.goods {
    import reprise.ui.*;
    import flash.events.*;
    import reprise.controls.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.world.vos.*;
    import __AS3__.vec.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.views.popups.*;
    import net.wooga.pets.constants.*;
    import net.wooga.pets.utils.*;
    import net.wooga.pets.models.world.*;

    public class GoodsPopupMediator extends AbstractPopupMediator {

        public var userModel:UserModel;
        public var contractsModel:ContractsModel;
        public var world:WorldModel;
        public var paymentModel:PaymentModel;
        private var _content:UIComponent;

        override public function onRegister():void{
            super.onRegister();
            RepriseUtils.addComponent(this.view, "titleBack");
            addLabel("title showcardGothicFont", "order_goods_headline", null, this.view);
            this._content = RepriseUtils.addComponent(this.view, "content");
            this.addProducts();
        }
        private function addProducts():void{
            var _local2:ContractOfferVO;
            var _local1:Vector.<ContractOfferVO> = this.contractsModel.contracts;
            for each (_local2 in _local1) {
                if (_local2.productId <= 0){
                    this.addProduct(_local2.code, _local2.goods, _local2.readyIn, _local2.coins, _local2.productSize);
                } else {
                    this.addFBCProduct(_local2.code, _local2.goods, _local2.productId, _local2.productSize);
                };
            };
        }
        private function addProduct(_arg1:String, _arg2:int, _arg3:int, _arg4:int, _arg5:String):void{
            var _local7:LabelButton;
            var _local6:LabelButton = RepriseUtils.addLabelButton(this._content, "myriadProFont product lightBG", "", this.onProductClick);
            _local6.setData({code:_arg1, amount:_arg2});
            RepriseUtils.addComponent(_local6, ("box" + _arg5.toUpperCase()));
            RepriseUtils.addLabel(_local6, "showcardGothicFont amount", ("<div class='amountX'>x</div>" + _arg2));
            RepriseUtils.addLabel(_local6, "time", language.getTextByKey("global_time"));
            RepriseUtils.addLabel(_local6, "time value", language.parseTreatmentTime(_arg3));
            RepriseUtils.addLabel(_local6, "cost", language.getTextByKey("global_costs"));
            RepriseUtils.addLabel(_local6, "cost value", _arg4.toString());
            RepriseUtils.addComponent(_local6, "cost coinIcon");
            var _local8:int = (GameConstants.MAX_GOODS_ITEM_NUM - this.userModel.goods);
            if (_arg2 > _local8){
                _local7 = RepriseUtils.addLabelButton(_local6, "greyButton button", language.getTextByKey("kiosk_stock_full"));
                _local7.enabled = false;
                _local6.enabled = false;
            } else {
                if (this.userModel.coins < _arg4){
                    _local7 = RepriseUtils.addLabelButton(_local6, "greyButton button", language.getTextByKey("global_no_coins"));
                    _local7.enabled = false;
                    _local6.enabled = false;
                } else {
                    _local7 = RepriseUtils.addLabelButton(_local6, "blueButton button", language.getTextByKey("order_goods_button"), this.onProductClick);
                    _local7.setData({code:_arg1, amount:_arg2});
                };
            };
        }
        private function addFBCProduct(_arg1:String, _arg2:int, _arg3:int, _arg4:String):void{
            var _local5:LabelButton = RepriseUtils.addLabelButton(this._content, "myriadProFont product", "", this.onFBProductClick);
            _local5.setData({id:_arg3});
            RepriseUtils.addComponent(_local5, ("box" + _arg4.toUpperCase()));
            RepriseUtils.addLabel(_local5, "showcardGothicFont amount", ("<div class='amountX'>x</div>" + this.getFBContract(_arg3).amount));
            var _local6:String = language.getTextByKey("global_button_pay_fb_credits", {amount:this.getFBContract(_arg3).price});
            var _local7:LabelButton = RepriseUtils.addLabelButton(_local5, "greenButton button", _local6, this.onFBProductClick);
            _local7.setData({id:_arg3});
            RepriseUtils.addComponent(_local7, "fbCreditsIcon");
        }
        private function getFBContract(_arg1:int):Object{
            var _local3:Object;
            var _local2:Array = (this.paymentModel.getData(ProtocolConstants.BUY_GOODS) as Array);
            for each (_local3 in _local2) {
                if (_local3.product_id == _arg1){
                    return (_local3);
                };
            };
            return (null);
        }
        private function onProductClick(_arg1:MouseEvent):void{
            var _local2:LabelButton = (_arg1.currentTarget as LabelButton);
            var _local3:String = this.world.getRoomIdsOfType(GameConstants.TYPE_DOCK)[0];
            dispatch(new InfoEvent(EventConstants.CREATE_CONTRACT, _local3, _local2.data()));
            close();
        }
        private function onFBProductClick(_arg1:MouseEvent):void{
            var _local2:LabelButton = (_arg1.currentTarget as LabelButton);
            dispatch(new InfoEvent(EventConstants.BUY_CONTRACT, _local2.data().id));
            close();
        }
        private function get view():GoodsPopupView{
            return ((viewComponent as GoodsPopupView));
        }

    }
}//package net.wooga.pets.views.popups.goods 
